package view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * A basic panel which just holds an image, but scaled to 50 percent of the size
 * This is necessary because of an issue that came up near the end of our design
 * where the store panel was not big enough to hold all of the items. Obviously, this is 
 * not an ideal solution.
 * 
 * @author Bill
 *
 */
public class ScaledImagePanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private ImageIcon image;
	private int numLayers = 0;
	private int rotation = 0;
	private final float scalingFactor = 0.6f;
	
	public ScaledImagePanel(String fileName){
		super();
		URL url = this.getClass().getResource("/graphics/"+fileName);
		System.out.println(fileName);
		image = new ImageIcon(url);
		image.setImage(image.getImage().getScaledInstance((int)((float)image.getIconWidth()*scalingFactor), (int)((float)image.getIconHeight()*scalingFactor), 0));
		this.setOpaque(false);
		setPreferredSize(new Dimension((int)((float)image.getIconWidth()*scalingFactor), (int)((float)image.getIconHeight()*scalingFactor)));
		setSize(new Dimension((int)((float)image.getIconWidth()*scalingFactor),(int)((float)image.getIconHeight()*scalingFactor)));
	}
	
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.drawImage(image.getImage(), 0, 0, null);
	}
	
	/**
	 * Adds an image on top of the current image
	 * @param fileName
	 */
	public void addImageLayer(String fileName){
		ScaledImagePanel newLayer = new ScaledImagePanel(fileName);
		this.add(newLayer);
		this.numLayers++;
		this.repaint();
	}
	
	/**
	 * removes the top image on the stack of layers, if there is more than zero layers
	 */
	public void removeTopImageLayer(){
		if(numLayers != 0){
			ScaledImagePanel topLayer = (ScaledImagePanel)this.getComponent(--numLayers);
			this.remove(topLayer);
			this.repaint();
		}
	}
	
	/**
	 * Getters and Setters
	 */
	public int getRotation() {
		return rotation;
	}

	public void setRotation(int rotation) {
		this.rotation = rotation;
	}
	
	public ImageIcon getImage() {
		return image;
	}

	public void setImage(ImageIcon image) {
		this.image = image;
	}

}
